package com.study;

import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;

/**
 * @author zhuzi
 * 2023/9/10 15:18
 */
public class LocalDateDemo {
    @Test
    public void test() {
        //指定转换格式
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate startDate = LocalDate.parse("2019-03-01", fmt);
        LocalDate endDate = LocalDate.parse("2020-04-02", fmt);

        Period period = Period.between(startDate, endDate);
        System.out.println("相差:" + period.getYears() + " 年 " + period.getMonths() + " 个月 " + period.getDays() + " 天");


        long until = startDate.until(endDate, ChronoUnit.DAYS);
        System.out.println("相差 天数"+until);
        System.out.println(startDate.until(endDate, ChronoUnit.YEARS));
    }

}
